var borrow_vm;
var invest_vm;
var borrowDetail;
var qishu = null;
$(function() {
	$("#startDate").val("2018-03-01 10:00:00");
	$("#endDate").val("2018-04-01 10:00:00");
	var couponIdVal = $("#coupon_id").val();
	borrowDetail = { }
	var hk_period={
			"1":"天",
			"2":"周",
			"3":"个月",
			"4":"年",
	}
	
	var hkfsType = {
            "1": "等额本息",
            "2": "等额本金",
            "3": "按月付息到期还本",
            "4": "一次性还款",
            "99": "其他"
     };
	
	$.ajax({
		async: false,
		type : "post",
		contentType : 'application/x-www-form-urlencoded;charset=utf-8',
		url : "../coupon/show_borrow_detail",
		data : {
			couponId : coupon_id
		},
		dataType : "json",
		success : function(r) {
			if (r.code == 0) {
				var borrow = r.page.list[0];
				borrowDetail.xiangmumingcheng = borrow.borrowName+borrow.borrowNo;
				borrowDetail.yuqinianhualilv = borrow.annualizedRate + "%";
				qishu = borrow.periodLength;
				borrowDetail.touziqixian = borrow.periodLength + hk_period[borrow.periodUnit];
				borrowDetail.huankuanfangsi = hkfsType[borrow.profitPlan];
				borrowDetail.touzijine = borrow.investAmount;
				borrowDetail.touzishijian = borrow.startDate;
				borrowDetail.yujishouyi = borrow.waitProfit;
				borrowDetail.leijishouyi = borrow.yesProfit;
				borrowDetail.zhuangtai = borrow.status==14?"已结清":"收款中";
				return true;
			}
		},
		error : function(r) {
				alert("用户信息查询失败,请刷新重试");
				console.log(r)
			}
	});
	borrow_vm = new Vue({
		el : '#borrow_info',
		data : {
			xiangmumingcheng : borrowDetail.xiangmumingcheng,
			yuqinianhualilv : borrowDetail.yuqinianhualilv,
			touziqixian : borrowDetail.touziqixian,
			huankuanfangsi : borrowDetail.huankuanfangsi,
			touzijine : borrowDetail.touzijine + "元",
			touzishijian : borrowDetail.touzishijian,
			yujishouyi : borrowDetail.yujishouyi  + "元",
			leijishouyi : borrowDetail.leijishouyi  + "元",
			zhuangtai : borrowDetail.zhuangtai
		},
		methods : {}
	});
	invest_vm = new Vue({
		el : '#autoTender',
		data : {
			tabsShow : true,
			detail : false,
			add : false,
			clicktag : 0,// 防重复提交
			couponId : couponIdVal
		},
		methods : {
			queryAutoTenderRecord : function() {
				console.log("click button");
				invest_vm.reload();
			},
			reload : function() {
				console.log(this.$refs.startDate.value);
				$("#jqGrid").setGridParam({
					postData : {
						userCode : this.userCode,
						startTime : this.startDate,
						endTime : this.endDate,
						tradeType : ''
					},
					page : 1
				}).trigger("reloadGrid");
			}
		}
	});

	$("#jqGrid").jqGrid({
		url : '../coupon/show_invest_detail',
		postData : {
			couponId : invest_vm.couponId
		},
		datatype : "json",
		colModel : [ {
			label : 'id',
			name : 'id',
			key : true,
			hidden : true
		}, {
			label : '期数',
			name : 'curStageNo',
			formatter: function(cellvalue, options, row){
				return row.curStageNo + '/' + qishu;
			}
				
		}, {
			label : '日期',
			name : 'receiveTime',
			formatter: function(cellvalue, options, row){
				var receiveTime = row.receiveTime;
				return receiveTime.substring(0,10);
			}
		}, {
			label : '应收金额',
			name : 'receiveAmount'
		}, {
			label : '应收本金',
			name : 'receiveCorpus'
		}, {
			label : '应收利息',
			name : 'receiveInterest'
		}, {
			label : '加息收益',
			name : 'couponInterest',
			width : 240
		}, {
			label : '状态',
			name : 'status',
			formatter : function(cellvalue, options, row) {
				var status = row.status;
				var hkStatus = {
					"1" : "待收款",
					"2" : "",
					"3" : "",
					"4" : "",
					"5" : "已收款",
					"6" : ""
				};
				return hkStatus[status];
			}
		} ],

		viewrecords : true,
		height : 500,
		rowNum : 100,
		rowList : [ 10, 30, 50 ],
		rownumbers : true,
		rownumWidth : 25,
		autowidth : true,
		multiselect : false,
		pager : "#jqGridPager",
		jsonReader : {
			root : "page.list",
			page : "page.currPage",
			total : "page.totalPage",// 总页数
			records : "page.totalCount"
		},
		prmNames : {
			page : "page",
			rows : "limit",
			order : "order"
		},
		gridComplete : function() {
			// 隐藏grid底部滚动条
			$("#jqGrid").closest(".ui-jqGrid-bdiv").css({
				"overflow-x" : "hidden"
			});
			$("#jqGridDetail").jqGrid("setGridWidth", $(window).width());
		}
	});
});
